<template>
  <div class="app-container">
    <el-form class="formList"  :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-row>
        <!-- <el-col :span="6"> -->
          <el-form-item label="模板名称" prop="moudleName" label-width="80px">
            <el-input v-model="queryParams.moudleName" placeholder="请输入模板名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :span="6"> -->
          <el-form-item label="状态" prop="dataStatus" label-width="68px">
            <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
              <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        <!-- </el-col> -->

        <!-- <el-col :span="5"> -->
          <el-form-item style="margin-left: 22px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        <!-- </el-col> -->
      </el-row>

    </el-form>

    <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['platform:moudle:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['platform:moudle:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['platform:moudle:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button v-if="0>1" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['platform:moudle:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
 <div class="divTable">
    <el-table v-loading="loading" border :data="moudleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="邮费模板名称" align="center" prop="moudleName" :show-overflow-tooltip="true" sortable />
      <el-table-column label="包邮条件金额" align="center" prop="moudleAmountFateStr" :show-overflow-tooltip="true" sortable />
      <el-table-column label="不满足条件金额" align="center" prop="freightInitFateStr" :show-overflow-tooltip="true"
        sortable />
      <el-table-column label="状态 " align="center" prop="dataStatus" sortable>
        <template slot-scope="scope">
          <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
            v-hasPermi="['platform:moudle:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:moudle:edit']">修改</el-button>
          <el-button v-if="scope.row.dataStatus == 0" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['platform:moudle:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
</div>
    <!-- 查看平台邮费模板对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-form ref="detailForm" :model="form" label-width="120px">
        <div>
          <div style="font-size: 16px; margin-bottom: 20px" class="big_title_style">基本信息
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="模板名称" width="400px">
                <span>{{ form.moudleName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="起始金额(元)" width="400px">
                <span>{{ form.moudleAmount }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="未满足邮费(元)" width="400px">
                <span>{{ form.freightInitFate}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="付费运费(元)" width="400px">
                <span>{{ form.platfreightInitFate }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="不配送区域理由" width="600px">
                <span>{{ form.reason }}</span>
              </el-form-item>
            </el-col>

          </el-row>
          <div style="font-size: 16px; margin-bottom: 20px;" class="big_title_style">包邮区域
          </div>
          <el-form-item label="">
            <span style="color: #606266;font-size: 14px;">
              {{oneArea}}
            </span>
          </el-form-item>
          <div style="font-size: 16px; margin-bottom: 20px;" class="big_title_style">付费区域

          </div>
          <el-form-item label="">
            <span style="color: #606266;font-size: 14px;">
              {{twoArea}}
            </span>
          </el-form-item>
          <div style="font-size: 16px; margin-bottom: 20px;" class="big_title_style">不配送区域

          </div>
          <el-form-item label="">
            <span style="color: #606266;font-size: 14px;">
              {{threeArea}}
            </span>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listMoudle,
    getMoudle,
    delMoudle,
    addMoudle,
    updateMoudle,
    updateMoudleStatus
  } from "@/api/platform/moudle";

  export default {
    name: "Moudle",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 平台邮费模板表格数据
        moudleList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          moudleName: null,
          moudleAmount: null,
          freightInitFate: null,
          plaftformUid: null,
          dataStatus: null,
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null,
          reason: null
        },
        columns: [{
            label: '邮费模板UID',
            dataIndex: 'moudleUid',
          },
          {
            label: '邮费模板名称',
            dataIndex: 'moudleName',
          },
          {
            label: '包邮条件金额',
            dataIndex: 'moudleAmount',
          },
          {
            label: '不满足条件金额',
            dataIndex: 'freightInitFate',
          },
          {
            label: '店铺ID',
            dataIndex: 'plaftformUid',
          },
          {
            label: '状态 1有效 0无效 -1 删除',
            dataIndex: 'dataStatus',
          },
          {
            label: '创建人id',
            dataIndex: 'createUid',
          },
          {
            label: '创建人',
            dataIndex: 'createName',
          },
          {
            label: '创建时间',
            dataIndex: 'createTime',
          },
          {
            label: '修改人ID',
            dataIndex: 'updateUid',
          },
          {
            label: '修改人',
            dataIndex: 'updateName',
          },
          {
            label: '修改时间',
            dataIndex: 'updateTime',
          },
          {
            label: '区域不配送的原因',
            dataIndex: 'reason',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        //常用状态数据字典
        statusOptions: [],
        oneArea: null,
        twoArea: null,
        threeArea: null
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.statusOptions = response.data;
      });
    },
    watch: {
      $route(to, from) {
        //from 从哪来
        //to //到哪去
        let path = from.path;
        if ("/platform/data/" == path) {
          //刷新页面
          this.getList();
        } else {
          let index = path.lastIndexOf("/");
          if (index > -1) {
            path = path.substr(0, index);
            if ("/platform/data" == path) {
              this.queryParams.pageNum = 1;
              //刷新页面
              this.getList();
            }
          }
        }
      }
    },
    methods: {
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm('确定要把模板名称为"' + row.moudleName + '",设为' + text + '吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateMoudleStatus(row.moudleUid, row.dataStatus);
        }).then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        }).catch(function() {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
      },
      /** 查询平台邮费模板列表 */
      getList() {
        this.loading = true;
        listMoudle(this.queryParams).then(response => {
          this.moudleList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          moudleUid: null,
          moudleName: null,
          moudleAmount: null,
          freightInitFate: null,
          plaftformUid: null,
          dataStatus: "0",
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null,
          reason: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.moudleUid)
        this.names = selection.map(item => item.moudleName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push("/platform/data/");
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看平台邮费模板";
        getMoudle(row.moudleUid).then(response => {
          this.form = response.data;
          if (this.form.freeShippingList) {
            let one = "";
            this.form.freeShippingList.forEach(vo => {
              if (vo) {
                one = one + "、" + vo;
              }
            })
            this.oneArea = one.substring(1, one.length);
          }
          if (this.form.aloeGelList) {
            let two = "";
            this.form.aloeGelList.forEach(vo => {
              if (vo) {
                two = two + "、" + vo;
              }
            })
            this.twoArea = two.substring(1, two.length);
          }
          if (this.form.noDeliveryList) {
            let three = "";
            this.form.noDeliveryList.forEach(vo => {
              if (vo) {
                three = three + "、" + vo;
              }
            })
            this.threeArea = three.substring(1, three.length);
          }
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const moudleUid = row.moudleUid || this.ids[0];

        this.$router.push("/platform/data/" + moudleUid);
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.moudleUid != null) {
              updateMoudle(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMoudle(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const moudleUids = row.moudleUid || this.ids;
        const names = row.moudleName || this.names;
        this.$confirm('是否确认删除模板名称为"' + names + '"的模板?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMoudle(moudleUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('platform/moudle/export', {
          ...this.queryParams
        }, `platform_moudle.xlsx`)
      }
    }
  };
</script>
